package com.hello.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hello.dto.PostAddDTO;
import com.hello.dto.PostLikeDTO;
import com.hello.entity.Follow;
import com.hello.entity.Post;
import com.hello.mapper.PostMapper;
import com.hello.service.IFollowService;
import com.hello.service.IPostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper,Post> implements IPostService {

    @Autowired
    private PostMapper postMapper;
    @Autowired
    private IFollowService followService;
    @Autowired
    private IFollowService followerService;
    /**
     * 返回PDI DTO列表
     * @param user_id
     * @return
     */

    @Override
    public  List<PostLikeDTO> getLikedList(Long user_id){

        List<PostLikeDTO> pdilist = postMapper.Select_PDI(user_id);
        return pdilist;

   }

    @Override
    public Post getPostDetail(Long host_id,Integer post_order){
        QueryWrapper<Post> wrapper = new QueryWrapper<Post>()
                .select("*")
                .eq("host_id",host_id)
                .eq("post_order",post_order);
        Post post = baseMapper.selectOne(wrapper);
        return post;
    }

    @Override
    public List<Post> queryPostList(){
        List<Post> posts = lambdaQuery().list();
        // 对帖子列表按照 postTime 倒序排序
        List<Post> sortedPosts = posts.stream()
                .sorted((post1, post2) -> post2.getPostTime().compareTo(post1.getPostTime()))
                .collect(Collectors.toList());

        return sortedPosts;
        //return posts;
    }

    @Override
    public List<Post> getFollowedPost(Long user_id){
        // 获取 user_id 关注的所有用户的 ID 列表
        List<Follow> followingList = followerService.getFollowing(user_id);

        //stream API实现倒序排序
        List<Post> allPosts =followingList.stream()
                .map(follow -> {
                    Long followingId = follow.getFollowingId();
                    QueryWrapper<Post> wrapper = new QueryWrapper<Post>()
                            .select("*")
                            .eq("host_id", followingId);
                    return postMapper.selectList(wrapper);
                })
                .flatMap(List::stream)
                .sorted(Comparator.comparing(Post::getPostTime).reversed())
                .collect(Collectors.toList());

        return allPosts;

    }

    

    @Override
    public boolean addPost(PostAddDTO postAddDTO) {
        Post post = new Post();
        BeanUtils.copyProperties(postAddDTO, post);
        post.setPostTime(LocalDateTime.now()); //设置当前时间为postTime
        post.setForwardingNum(0);
        //获取post数据库的post_order最大值max_post_order并将当前插入的帖子的Order设置为max_post_order+1
        post.setPostOrder(0);
        Integer maxPostOrder = baseMapper.getMaxPostOrder();
        post.setPostOrder(maxPostOrder != null ? maxPostOrder + 1 : 1); // 如果数据库为空，则设置为1
        int num = baseMapper.insert(post);
        return num>0?true:false;
    }
}
